﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using QLNHang.BUS;
using QLNHang.DTO;

namespace QLNHang.GUI
{
    public partial class ucThemHoaDon : UserControl
    {
        double tongTien = 0;
        public ucThemHoaDon()
        {
            InitializeComponent();
            gvVe.Columns["DoiTuong"].ReadOnly = true;
            gvVe.Columns["LoaiVe"].ReadOnly = true;
            gvVe.Columns["ThoiGian"].ReadOnly = true;
            gvVe.Columns["Gia"].ReadOnly = true;
            GridLoadVe();
            GridLoadKH();
            btnBanVe.Enabled = false;
        }
        private void gvVe_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            gvVe.ClearSelection();
            gvVe.CurrentCell = null;
        }
        private void GridLoadVe()
        {
            gvVe.DataSource = new VeBUS().XemDanhSachVe();

        }
        private void GridLoadKH()
        {
            KhachHangDTO kh = new KhachHangDTO();
            gvKhachHang.DataSource = new KhachHangBUS().XemKhachHang(kh);

        }
        private void gvKhachHang_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            gvKhachHang.ClearSelection();
            gvKhachHang.CurrentCell = null;
            txtHoTen.Focus();
        }

        private void btnTimKH_Click(object sender, EventArgs e)
        {
            KhachHangDTO kh = new KhachHangDTO();
            kh.HoTenKhachHang = txtHoTen.Text.Trim();
            kh.DiaChi = txtDiaChi.Text.Trim();
            kh.SoDienThoai = txtSoDienThoai.Text.Trim();
            kh.SoCMND = txtCMND.Text.Trim();
            gvKhachHang.DataSource = new KhachHangBUS().XemKhachHang(kh);

        }

        private void btnBanVe_Click(object sender, EventArgs e)
        {
            try
            {
                HoaDonDTO hoaDonDTO = new HoaDonDTO();
                VeDTO veDTO = new VeDTO();
                var selectedRow = gvKhachHang.SelectedRows[0];
                if (selectedRow != null)
                {
                    hoaDonDTO.MaKhachHang = selectedRow.Cells["MaKH"].Value.ToString();
                    foreach (DataGridViewRow rows in gvVe.Rows)
                    {
                        if (rows.Cells["SoLuong"].Value.ToString() != "0")
                        {
                            hoaDonDTO.SoLuong = rows.Cells["SoLuong"].Value.ToString();
                            hoaDonDTO.MaVe = rows.Cells["MaVe"].Value.ToString();
                            new HoaDonBUS().ThemHoaDon(hoaDonDTO);
                        }
                    }
                }
                if (tongTien >= 500000 && tongTien < 1000000)
                {
                    hoaDonDTO.DiemTichLuy = "10";
                }
                else if (tongTien > 1000000 && tongTien <= 2000000)
                {
                    hoaDonDTO.DiemTichLuy = "25";
                }
                else if (tongTien > 2000000 && tongTien <= 3000000)
                {
                    hoaDonDTO.DiemTichLuy = "40";
                }
                else if (tongTien > 3000000 && tongTien <= 4000000)
                {

                    hoaDonDTO.DiemTichLuy = "55";
                }
                else if (tongTien > 4000000 && tongTien <= 5000000)
                {

                    hoaDonDTO.DiemTichLuy = "70";
                }
                else if (tongTien > 5000000 && tongTien <= 6000000)
                {

                    hoaDonDTO.DiemTichLuy = "90";
                }
                else if (tongTien > 6000000)
                {

                    hoaDonDTO.DiemTichLuy = "120";
                }

                new HoaDonBUS().CapNhatDiemTichLuy(hoaDonDTO);
                MessageBox.Show("Bán vé thành công");
                resetControl();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            resetControl();
           
        }
        private void resetControl()
        {
            gvKhachHang.ClearSelection();
            gvKhachHang.CurrentCell = null;
            gvVe.ClearSelection();
            gvVe.CurrentCell = null;
            lbTongTien.Text = "";
            tongTien = 0;
            btnBanVe.Enabled = false;
            GridLoadKH();
            GridLoadVe();
        }

        private void gvKhachHang_MouseClick(object sender, MouseEventArgs e)
        {
            KhachHangDTO khDTO = new KhachHangDTO();
            HoaDonDTO hoaDonDTO = new HoaDonDTO();
           
            var selectedRow = gvKhachHang.SelectedRows[0];
            if (selectedRow != null)
            {

                btnBanVe.Enabled = true;
                hoaDonDTO.DiemTichLuy = selectedRow.Cells["DiemTichLuy"].Value.ToString();
                if (!string.IsNullOrEmpty(hoaDonDTO.DiemTichLuy))
                {
                    if (Convert.ToInt32(hoaDonDTO.DiemTichLuy) == 20)
                    {
                        lbTongTien.Text = convertGia(tongTien * 0.95);
                    }
                    else if (Convert.ToInt32(hoaDonDTO.DiemTichLuy) == 100)
                    {
                        lbTongTien.Text = convertGia((tongTien * 0.9));
                    }
                    else if (Convert.ToInt32(hoaDonDTO.DiemTichLuy) >= 200)
                    {
                        lbTongTien.Text = convertGia(tongTien * 0.85);
                    }
                }
            }
            else
            {
                btnBanVe.Enabled = false;
            }
        }


        private void gvVe_SelectionChanged(object sender, EventArgs e)
        {

            lbTongTien.Text = convertGia(calculate());
        }
        private double calculate()
        {
            tongTien = 0;
            string SoLuong = "0";
            string GiaVe = "0";
            foreach (DataGridViewRow rows in gvVe.Rows)
            {
                if (rows.Cells["SoLuong"].Value.ToString() != "0")
                {
                    SoLuong = rows.Cells["SoLuong"].Value.ToString();
                    GiaVe = rows.Cells["Gia"].Value.ToString();
                    tongTien = tongTien + Convert.ToDouble(GiaVe) * Convert.ToDouble(SoLuong);
                }
            }
            return tongTien;
        }
        private string convertGia(double gia)
        {
            return string.Format("{0:##,###.##}", gia).Replace('.', '*').Replace(',', '.').Replace('*', ',') + " VNĐ";
        }
    }
}
